/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.web;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.boxin.ims.modules.wechat.entity.Witticism;
import com.boxin.ims.modules.wechat.service.WitticismService;
import com.boxin.ims.modules.wechat.utils.WechatUtils;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.FileUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 俏皮话Controller
 * @author Jakemanse
 * @version 2013-11-14
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/wechat/witticism")
public class WitticismController extends BaseController {

	@Autowired
	private WitticismService witticismService;
	
	@ModelAttribute
	public Witticism get(@RequestParam(required=false) Long id) {
		if (id != null){
			return witticismService.get(id);
		}else{
			return new Witticism();
		}
	}
	
	@RequiresPermissions("wechat:witticism:view")
	@RequestMapping(value = {"list", ""})
	public String list(Witticism witticism, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			witticism.setUser(user);
		}
        Page<Witticism> page = witticismService.find(new Page<Witticism>(request, response), witticism); 
        model.addAttribute("page", page);
		return "modules/wechat/witticismList";
	}

	@RequiresPermissions("wechat:witticism:view")
	@RequestMapping(value = "form")
	public String form(Witticism witticism, Model model) {
		
		User user = UserUtils.getUser();
		if(witticism.getUser() == null || witticism.getUser().getId() ==0){
			witticism.setUser(user);
		}
		
		model.addAttribute("witticism", witticism);
		return "modules/wechat/witticismForm";
	}

	@RequiresPermissions("wechat:witticism:edit")
	@RequestMapping(value = "save")
	public String save(@RequestParam("picFile") MultipartFile picFile,Witticism witticism, Model model, RedirectAttributes redirectAttributes,HttpServletRequest request) {
		if (!beanValidator(model, witticism)){
			return form(witticism, model);
		}
		
		String unicode=request.getParameter("unicode");
		String url=request.getParameter("url");
		if(StringUtils.isEmpty(unicode)){
			unicode="utf-8";
		}
		
		if(picFile != null && picFile.getOriginalFilename().length()>4){
			String wpPath = WechatUtils.getWechatRegMemberSavePath(request);
			String sname = picFile.getOriginalFilename();
			String fileType = sname.substring(sname.lastIndexOf("."));
			String fname  = UserUtils.getUser().getLoginName()+	"_wp_"+System.currentTimeMillis()+fileType;
			File file = new File(wpPath+File.separator +fname);
			System.out.println("保存文件:"+wpPath+File.separator +fname);
			try {
				picFile.transferTo(file);
				witticism.setPicUrl("http://weixin.zhaotang.net/zt/userfiles/images/"+file.getName());
			} catch (Exception e) {
				System.out.println("上传文件失败:"+wpPath+fname);
				e.printStackTrace();
			}
			
		}
		
		
		witticismService.save(witticism);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/witticism/?repage";
	}
	
	@RequiresPermissions("wechat:witticism:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		witticismService.delete(id);
		addMessage(redirectAttributes, "删除俏皮话成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/witticism/?repage";
	}
	
}
